package com.lry.basic.algorithm.tree;



import java.util.*;

public class BSTTree<E extends Comparable<E>> {

    private class Node{
        public E e;
        public Node left,right;
        public Node(E e){
            this.e = e;
        }
    }

    private Node root;

    private int size;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size==0;
    }

    //非递归
    public boolean addNR(E e){
        if(null==e){
            return false;
        }
        if(null==root){
            root = new Node(e);
            size++;
            return true;
        }
        Node temp = null;
        Node cur = root;
        while(cur!=null){
            temp = cur;
            if(e.compareTo(cur.e)==0){
                return false;
            }else if(e.compareTo(cur.e)<0){
                cur = cur.left;
                if(null==cur){
                    size++;
                    temp.left = new Node(e);
                }
            }else{
                cur = cur.right;
                if(null==cur){
                    size++;
                    temp.right = new Node(e);
                }
            }
        }
        return true;
    }

    public boolean add(E e) {
        if(null==e){
            return false;
        }
        int tempSize = size;
        root = add(root,e);

        return size==tempSize+1;
    }

    private Node add(Node node,E e){
        if(null==node){
            size++;
            return new Node(e);
        }

        if(e.compareTo(node.e)<0){
            node.left = add(node.left,e);
        }else if(e.compareTo(node.e)>0){
            node.right = add(node.right,e);
        }
        return node;
    }

    public boolean containsNR(E e){
        if(null==e){
            return false;
        }
        if(null==root)
            return false;

        Node cur = root;
        while(cur!=null){
            if(e.compareTo(cur.e)==0){
                return true;
            }else if(e.compareTo(cur.e)<0){
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return false;
    }

    public boolean contains(E e) {
        if(null==e){
            return false;
        }
        return contains(root,e);
    }


    private boolean contains(Node node, E e) {
        if(null==node){
            return false;
        }

        if(e.compareTo(node.e)<0){
            return contains(node.left,e);
        }else if(e.compareTo(node.e)>0){
            return contains(node.right,e);
        }else{
            return true;
        }
    }


    public List<E> preOrder(){
        List<E> list = new ArrayList<>();
        preOrder(root,list);
        return list;
    }

    private void preOrder(Node node,List<E> list){
        if(null==node)
            return;
        list.add(node.e);
        preOrder(node.left,list);
        preOrder(node.right,list);
    }

    public List<E> preOrderNR() {
        List<E> list = new ArrayList<>();
        Stack<Node> stack = new Stack();
        stack.push(root);
        while(!stack.isEmpty()){
            Node cur = stack.pop();
            list.add(cur.e);

            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }
        return list;
    }



    public List<E> inOrder(){
        List<E> list = new ArrayList<>();
        inOrder(root,list);
        return list;
    }

    private void inOrder(Node node,List<E> list){
        if(null==node)
            return;
        preOrder(node.left,list);
        list.add(node.e);
        preOrder(node.right,list);
    }

    public List<E> inOrderNR() {
        List<E> list = new ArrayList<>();
        Stack<Node> stack = new Stack();
        Node cur = root;
        while(!stack.isEmpty()||cur!=null){

            while(cur!=null){
                stack.push(cur);
                cur = cur.left;
            }
            //左边走完了，cur==null
            if(!stack.isEmpty()){
                cur = stack.pop();
                list.add(cur.e);
                cur = cur.right;
            }

        }
        return list;
    }

    public List<E> postOrder(){
        List<E> list = new ArrayList<>();
        postOrder(root,list);
        return list;
    }

    private void postOrder(Node node,List<E> list){
        if(null==node)
            return;
        preOrder(node.left,list);
        preOrder(node.right,list);
        list.add(node.e);
    }

    public List<E> levelOrder(){
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        List<E> list = new ArrayList<>();
        while(!queue.isEmpty()){
             Node cur = queue.poll();
             list.add(cur.e);
              if(cur.left!=null){
                  queue.add(cur.left);
              }
              if(cur.right!=null){
                  queue.add(cur.right);
              }
        }
        return list;
    }

    public E removeMin(){
        E min = min();
        root = removeMin(root);
        return min;
    }

    private Node removeMin(Node node) {
        if(node.left==null){
           Node right = node.right;
           size--;
           node.right = null;
           return right;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public E removeMax(){
        E max = max();
        root = removeMax(root);
        return max;
    }

    private Node removeMax(Node node) {
        if(node.right==null){
            Node left = node.left;
            size--;
            node.left = null;
            return left;
        }
        node.right = removeMax(node.right);
        return node;
    }


    /**
     * 删除最小值所在节点，返回最小值
     * @return
     */
    public E removeMinNR(){
        E min = min();

        Node cur = root;
        Node par = cur;
        while(cur.left!=null){
            par = cur;
            cur = cur.left;
        }
        //cur是最小值，par是cur的父亲
        if(cur==root){
            root=null;
        }else{
            par.left = cur.right;
        }
        size--;
        return min;
    }

    /**
     * 删除最大值所在节点，返回最大值
     * @return
     */
    public E removeMaxNR(){
        E max = max();

        Node cur = root;
        Node par = cur;
        while(cur.right!=null){
            par = cur;
            cur = cur.right;
        }
        //cur是最小值，par是cur的父亲
        if(cur==root){
            root=null;
        }else{
            par.right = cur.left;
        }
        size--;
        return max;
    }

    public Node parentNR(E e){
        if(null==e){
            return null;
        }
        Node cur = root;
        Node par = null;
        while(cur!=null){
            if(e.compareTo(cur.e)<0){
                par = cur;
                cur = cur.left;
            }else if(e.compareTo(cur.e)>0){
                par = cur;
                cur = cur.right;
            }else{
                return par;
            }
        }
        return null;
    }


    public boolean remove(E e) {
        if(null==e)
            return false;
        int tempSize = size;
        root = remove(root,e);
        return size == tempSize-1;
    }

    private Node remove(Node node, E e) {
        if(null==node){
            return null;
        }

        if(e.compareTo(node.e)<0){
             node.left = remove(node.left,e);
             return node;
        }else if(e.compareTo(node.e)>0){
            node.right = remove(node.right,e);
            return node;
        }else{
            //待删除节点右子树为空
            if(node.right==null){
               Node left =  node.left;
               size--;
               node.left = null;
               return left;
            }
            //待删除节点左子树为空
            if(node.left==null){
                Node right =  node.right;
                size--;
                node.right = null;
                return right;
            }

            // 待删除节点左右子树均不为空的情况
            // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
            // 用这个节点顶替待删除节点的位置

            Node successor = min(node.right);

            successor.right = removeMin(node.right);

            successor.left = node.left;

            node.left = node.right = null;
            return successor;
        }
    }

    public E min(){
        if(size == 0)
            throw new IllegalArgumentException("BST is empty!");
        return min(root).e;
    }

    private Node min(Node node){
        if(node.left==null)
            return node;
        return min(node.left);
    }

    public E max(){
        if(size == 0)
            throw new IllegalArgumentException("BST is empty!");
        return max(root).e;
    }

    private Node max(Node node){
        if(node.right==null)
            return node;
        return max(node.right);
    }


    public List<String> path(){
        List<String> res = new ArrayList<>();
        if(size==0)
            return res;
        path(root,"",res);
        return res;
    }

    private void path(Node node, String s, List<String> res) {
        if(node.left==null&&node.right==null){
            res.add(s+node.e);
        }
        if(node.left!=null){
            path(node.left,s+node.e+"->",res);
        }
        if(node.right!=null){
            path(node.right,s+node.e+"->",res);
        }
    }

    //找到pq最近公共祖先
    public Node lowestCommonAncestor(E p, E q){
        return lowestCommonAncestor(root,p,q);
    }

    public Node lowestCommonAncestor(Node node,E p, E q){
        //pq全在右子树
        //pq全在左子树
        //else 找到了
        if(p.compareTo(node.e)>0&&q.compareTo(node.e)>0){
            return lowestCommonAncestor(node.right,p,q);
        }else if(p.compareTo(node.e)<0&&q.compareTo(node.e)<0){
            return lowestCommonAncestor(node.left,p,q);
        }else{
            return node;
        }
    }



    public Node removeMinNode(Node node){
        if(null==node.left){
            Node right = node.right;
            node.right = null;
            return right;
        }
        node.left = removeMinNode(node.left);
        return node;
    }

    public Node minNode(Node node){
        if(null==node.left){
            return node;
        }
        return minNode(node.left);
    }

    public Node deleteNode(Node root, E key) {
        if(null==root){
            return null;
        }

        if(key.compareTo(root.e)>0){
            root.right = deleteNode(root.right,key);
            return root;
        }else if(key.compareTo(root.e)<0){
            root.left = deleteNode(root.left,key);
            return root;
        }else{
            //找到了待删除结点
            if(null==root.left){
                Node right = root.right;
                root.right = null;
                return right;
            }else if(null==root.right){
                Node left = root.left;
                root.left = null;
                return left;
            }else{
                //待删除节点左右孩子都不为空，去右子树找最小节点代替待删除结点
                Node successor = minNode(root.right);


                successor.right = removeMinNode(root.right);

                successor.left = root.left;

                root.left = null;
                root.right = null;

                return successor;
            }
        }
    }

    public static void main(String[] args) {
//        BSTTree<Integer> tree = new BSTTree<>();
//        tree.addNR(2);
//        tree.addNR(1);
//        tree.addNR(3);
//        System.out.println(tree.path());
//        tree.removeMax();
//        System.out.println(tree.path());
//
//        tree.remove(2);
//        System.out.println(tree.path());
//
//        tree.remove(1);
//        System.out.println(tree.path());

        BSTTree<Integer> tree = new BSTTree<>();
        tree.addNR(5);
        tree.addNR(3);
        tree.addNR(6);
        tree.addNR(2);
        tree.addNR(4);
        tree.addNR(7);
        System.out.println(tree.path());
        tree.remove(tree.root,3);
        System.out.println(tree.path());
    }


}
